package com.huizu.project.service.impl;

import java.util.Date;
import java.util.List;

import com.huizu.common.enums.LaborEnum;
import com.huizu.common.exception.ServiceException;
import com.huizu.common.utils.DateUtils;
import com.huizu.common.utils.SnowFlake;
import com.huizu.project.domain.*;
import com.huizu.project.mapper.ProFileMapper;
import com.huizu.project.mapper.ProLdFsMapper;
import com.huizu.project.mapper.ProUserMapper;
import com.huizu.project.service.IBurialPointService;
import com.huizu.project.service.IProLdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huizu.project.mapper.ProLdgyMapper;
import com.huizu.project.service.IProLdgyService;

import static com.huizu.common.utils.PageUtils.startPage;

/**
 * 社会公益劳动Service业务层处理
 * 
 * @author zhangmx
 * @date 2024-06-05
 */
@Service
public class ProLdgyServiceImpl implements IProLdgyService 
{
    @Autowired
    private ProLdgyMapper proLdgyMapper;

    @Autowired
    private ProLdFsMapper proLdFsMapper;

    @Autowired
    private IProLdService iProLdService;

    @Autowired
    private ProFileMapper proFileMapper;

    @Autowired
    private ProUserMapper proUserMapper;

    @Autowired
    private IBurialPointService burialPointService;

    /**
     * 查询社会公益劳动
     * 
     * @param id 社会公益劳动主键
     * @return 社会公益劳动
     */
    @Override
    public ProLdgy selectProLdgyById(String id)
    {
        ProLdgy proLdgy = proLdgyMapper.selectProLdgyById(id);
        proLdgy.setProjectName("劳动公益");
        ProFile proFile = new ProFile();
        proFile.setBusinessId(id);
        proLdgy.setProFileList(proFileMapper.selectProFileList(proFile));
        return proLdgy;
    }

    /**
     * 查询社会公益劳动列表
     * 
     * @param proLdgy 社会公益劳动
     * @return 社会公益劳动
     */
    @Override
    public List<ProLdgy> selectProLdgyList(ProLdgy proLdgy)
    {
        return proLdgyMapper.selectProLdgyList(proLdgy);
    }

    /**
     * 新增社会公益劳动
     * 
     * @param proLdgy 社会公益劳动
     * @return 结果
     */
    @Override
    public int insertProLdgy(ProLdgy proLdgy)
    {

        proLdgy.setCreateTime(DateUtils.getNowDate());
        return proLdgyMapper.insertProLdgy(proLdgy);
    }

    /**
     * 修改社会公益劳动
     * 
     * @param proLdgy 社会公益劳动
     * @return 结果
     */
    @Override
    public int updateProLdgy(ProLdgy proLdgy)
    {
        return proLdgyMapper.updateProLdgy(proLdgy);
    }

    /**
     * 批量删除社会公益劳动
     * 
     * @param ids 需要删除的社会公益劳动主键
     * @return 结果
     */
    @Override
    public int deleteProLdgyByIds(String[] ids)
    {
        return proLdgyMapper.deleteProLdgyByIds(ids);
    }

    /**
     * 删除社会公益劳动信息
     * 
     * @param id 社会公益劳动主键
     * @return 结果
     */
    @Override
    public int deleteProLdgyById(String id)
    {
        return proLdgyMapper.deleteProLdgyById(id);
    }


    public List<ProLdgy> selectProLdgyListApi(ProLdgy proLdgy){
        startPage();
        return proLdgyMapper.selectProLdgyList(proLdgy);
    }

    /**
     * 新增社会公益劳动
     *
     * @param proLdgy 社会公益劳动
     * @return 结果
     */
    public int insertProLdgyApi(ProLdgy proLdgy){
        proLdgy.setId(String.valueOf(SnowFlake.nextId()));
        proLdgy.setCreateTime(new Date());
        proLdgy.setStatus("1");
        iProLdService.insertProFiles(proLdgy.getUserId(),proLdgy.getId(),"ldgy",proLdgy.getProFileList());
        int row =  proLdgyMapper.insertProLdgy(proLdgy);

        // 添加埋点
        ProUser proUser = proUserMapper.selectProUserByUserId(proLdgy.getUserId());
        if (proUser == null) {
            throw new ServiceException("未查询到此学生信息");
        }
        BurialPoint burialPoint = BurialPoint.builder()
                .userId(proLdgy.getUserId())
                .type(LaborEnum.SOCIAL_PUBLIC_WELFARE.getValue())
                .businessId(proLdgy.getId())
                .xyId(proUser.getXyId())
                .build();
        burialPointService.insertBurialPoint(burialPoint);

        return row;
    }

    public int updateProLdgyApi(ProLdgy proLdgy){
        proFileMapper.deleteProFileByBusinessId(proLdgy.getId());
        iProLdService.insertProFiles(proLdgy.getUserId(),proLdgy.getId(),"ldgy",proLdgy.getProFileList());
        proLdgy.setCreateTime(new Date());
        proLdgy.setStatus("1");
        return proLdgyMapper.updateProLdgy(proLdgy);
    }

    public int pass(ProLdgy proLdPram){
        ProLdgy proLd = proLdgyMapper.selectProLdgyById(proLdPram.getId());
        ProLdFs proLdFs = proLdFsMapper.selectProLdFsById(proLdPram.getFsId());
        if (proLdFs==null)
            throw new ServiceException("请选择正确的审批分数");
        proLdPram.setStatus("2");
        proLdPram.setDfCount(proLdFs.getContent());
        proLdPram.setMarkNumber(proLdFs.getName());
        proLdPram.setOpTime(new Date());

        return proLdgyMapper.updateProLdgy(proLdPram);
    }

    public int reject(ProLdgy proLd){
        proLd.setStatus("3");
        proLd.setOpTime(new Date());
        return proLdgyMapper.updateProLdgy(proLd);
    }

    @Override
    public int repulse(ProLdgy proLd) {
        ProLdgy proLdgy = new ProLdgy();

        proLdgy.setId(proLd.getId());
        // 设置为待审核状态
        proLdgy.setStatus("3");
        proLdgy.setOpTime(new Date());
        return proLdgyMapper.updateRepulse(proLdgy);
    }
}
